package com.zmovie.admin.controller.cinema;

import cn.hutool.core.util.StrUtil;
import com.zmovie.admin.constant.Constants;
import com.zmovie.common.error.BusinessException;
import com.zmovie.common.error.EnumBusinessError;
import com.zmovie.common.response.CommonReturnType;
import com.zmovie.framework.domain.CinemaArea;
import com.zmovie.framework.service.CinemaAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec 电影院区域模块
 * @since 2023-11-30 12:00
 */
@Controller
@RequestMapping("/cinema/area")
@Api(tags = "电影院区域模块")
public class CinemaAreaController {
    @Autowired
    private CinemaAreaService cinemaAreaService;
    /********地区********/
    @GetMapping
    @ApiOperation("电影院地区页面")
    public String index(CinemaArea cinemaArea, Model model){
        //条件查询
        List<CinemaArea> cinemaAreas = cinemaAreaService.listCinemaAreas(cinemaArea);
        model.addAttribute(Constants.CINEMA_AREAS_KEY, cinemaAreas);
        return "cinema/cinema_area";
    }

    @PostMapping("/search")
    @ApiOperation("条件查询区域")
    public Object searchArea(CinemaArea area, Model model){
        List<CinemaArea> cinemaAreas = cinemaAreaService.listCinemaAreas(area);
        model.addAttribute(Constants.CINEMA_AREAS_KEY, cinemaAreas);
        //返回渲染后的表格
        return "cinema/cinema_area :: areaList";
    }
    @GetMapping("/{id}")
    @ApiOperation("获取电影院地区信息")
    @ResponseBody
    public Object getArea(@PathVariable("id") Integer id){
        return CommonReturnType.success(cinemaAreaService.getById(id));
    }

    @PostMapping
    @ApiOperation("添加电影院区域")
    @ResponseBody
    public Object addArea(CinemaArea area) throws BusinessException {
        //参数校验
        if(StrUtil.isEmpty(area.getName()) || StrUtil.isEmpty(area.getProvince()) || StrUtil.isEmpty(area.getCity())){
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        try {
            boolean save = cinemaAreaService.save(area);
            if(!save){
                return CommonReturnType.fail("添加失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return CommonReturnType.fail("添加失败！");
        }
        return CommonReturnType.success("添加成功!");
    }

    @PutMapping
    @ApiOperation("修改电影院区域")
    @ResponseBody
    public Object updateArea(CinemaArea area) throws BusinessException {
        //参数校验
        if(area.getId() == null || StrUtil.isEmpty(area.getName()) || StrUtil.isEmpty(area.getProvince()) || StrUtil.isEmpty(area.getCity())){
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        cinemaAreaService.updateById(area);
        return CommonReturnType.success("修改成功!");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("修改电影院区域")
    @ResponseBody
    public Object deleteArea(@PathVariable("id") Integer id) throws BusinessException {
        //参数校验
        if(id == null) {
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        cinemaAreaService.removeById(id);
        return  CommonReturnType.success("删除成功!");
    }

    @DeleteMapping
    @ApiOperation("批量删除电影院区域")
    @ResponseBody
    public Object deleteAreaBatch(Integer[] id) throws BusinessException {
        //参数校验
        if(id == null || id.length == 0) {
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        cinemaAreaService.removeByIds(Arrays.asList(id));
        return  CommonReturnType.success("批量删除成功!");
    }
}
